package com.jinzhuo.cobtroller;

import com.jinzhuo.pojo.user;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.jinzhuo.rabbitmq_config.Mq_provider.EXCHANGE_NAME;
import static com.jinzhuo.rabbitmq_config.Mq_provider.QUEUE_NAME;

@RestController
public class sendController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 向队列中发送一条消息
     * @param user
     * @throws Exception
     */
    @RequestMapping(value = "sendMessage")
    public void sendMessage(user user) throws Exception {
        if ("".equals(user.getPassword())){
            throw new Exception("没有输入密码");
        }
        String message="恭喜你已注册成功";
        rabbitTemplate.convertAndSend(EXCHANGE_NAME,"queue1",message);
    }

    /**
     * 就收消息
     * @param message
     * @return
     */
    @RequestMapping(value = "recieveMessage")
    @RabbitListener(queues = QUEUE_NAME)
    public String recieveMessage(Message message){
        String message2 = message.toString();
        return message2;
    }
}
